package com.robot.controller;

import com.robot.dao.UserMapper;
import com.robot.model.Project;
import com.robot.model.ProjectMember;
import com.robot.model.User;
import com.robot.service.ProjectService;
import com.robot.util.SessionUtil;
import com.robot.vo.ResponseMessage;
import com.robot.vo.TablePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/6/23
 * Time: 17:53
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    ProjectService projectService;
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/page")
    public ModelAndView page(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/project/projectList");
        return modelAndView;
    }
    @RequestMapping("/projectMemberPage")
    public ModelAndView memberPage(Integer projectId){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/project/memberPage");
        modelAndView.addObject("projectId",projectId);
        return modelAndView;
    }
    @RequestMapping("/saveProject")
    public @ResponseBody
    ResponseMessage saveProject(@Valid Project project, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
           String msg= bindingResult.getFieldError().getDefaultMessage();
           return ResponseMessage.errorResponse(msg);
        }
        Integer id = project.getId();
        if(null == id){
            User userDo = (User) SessionUtil.getSessionUser();
            Integer userId = userDo.getId();
            return projectService.addProject(project, userId);
        }else {
            return projectService.modifyProject(project);
        }
    }

    @RequestMapping("/getProject")
    public @ResponseBody
    ResponseMessage getProject(Project project) {
        ResponseMessage responseMessage= projectService.getProjectList(project);
//        return  (TablePage)responseMessage.getData();
        return projectService.getProjectList(project);
    }

    @RequestMapping("/addProjectMember")
    public @ResponseBody
    ResponseMessage addProjectMember(@Valid ProjectMember projectMember,BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
           String msg= bindingResult.getFieldError().getDefaultMessage();
           return ResponseMessage.errorResponse(msg);
        }
        return projectService.addProjectMember(projectMember);
    }

    @RequestMapping("/removeUserFromProject")
    public @ResponseBody
    ResponseMessage removeUserFromProject(ProjectMember projectMember) {
        return projectService.removeUserFromProject(projectMember);
    }

    @RequestMapping("/projectMember")
    public @ResponseBody
    ResponseMessage projectMemberList(ProjectMember projectMember) {
        return projectService.getProjectMemberList(projectMember);
    }
    @RequestMapping("/switchProject")
    public ModelAndView switchProject(Integer projectId){
       User user= SessionUtil.getSessionUser();
       user.setLastProjectId(projectId);
       userMapper.modifyUser(user);
       ModelAndView modelAndView=new ModelAndView();
       modelAndView.setViewName("redirect:/index");
       return modelAndView;
    }
}
